package com.starmark.gateway.springcloud.provider.swagger;


import com.starmark.gateway.springcloud.provider.constants.GatewayConstant;
import com.starmark.gateway.springcloud.provider.dto.GatewayAppDto;
import com.starmark.gateway.springcloud.provider.service.IRouteService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;

import java.util.List;
import java.util.stream.Collectors;


@Component
@Primary
@AllArgsConstructor
public class SwaggerProvider {


    @Autowired
    private IRouteService routeService;

    public List<SwaggerResource> get(String projectCode) {
        //获取真正的projectCode
        String innerProjectCode = projectCode.replace(GatewayConstant.STARMARK_SWAGGER, "");
        List<GatewayAppDto> appDtos = routeService.getGatewayApps(innerProjectCode);

        List<SwaggerResource> resources = appDtos.stream().map(this::swaggerResource).collect(Collectors.toList());
        return resources;
    }

    private SwaggerResource swaggerResource(GatewayAppDto gatewayAppVo) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(gatewayAppVo.getAppName());
        swaggerResource.setLocation("/v2/api-docs?swagger_code="+gatewayAppVo.getAppCode());
        swaggerResource.setSwaggerVersion("2.0");
        return swaggerResource;

    }


}
